﻿using System;
using System.Collections.Generic;
using System.Globalization;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface IRatingsView : IAbstractView
    {
        DateTime Start { get; }
        DateTime End { get; }
        byte[] FileForDownload { get; set; }
    }

    public partial class Ratings : ViewPage<IRatingsView, RatingsPresenter>, IRatingsView
    {
        public List<RatingView> RatingsView { get; set; }

        #region IRatingsView Members

        public string DepartmentName { set; private get; }

        public DateTime Start
        {
            get { return !string.IsNullOrEmpty(datefrom.Text) ? DateTime.Parse(datefrom.Text) : DateTime.Now.Date; }
            set { datefrom.Value = value.ToString("d", CultureInfo.InvariantCulture); }
        }

        public DateTime End
        {
            get { return !string.IsNullOrEmpty(dateto.Text) ? DateTime.Parse(dateto.Text) : DateTime.Now.Date.AddDays(1); }
            set { dateto.Value = value.ToString("d", CultureInfo.InvariantCulture); }
        }

        public byte[] FileForDownload { get; set; }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack){
                Start = DateTime.Now.Date;
                End = DateTime.Now.Date.AddDays(1);
            }
        }

        protected void CreateReport(object sender, EventArgs eventArgs)
        {
            Presenter.OnCreateReport();
            DumpExcel();
        }

        private void DumpExcel()
        {
            Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;  filename=RatingReport.xlsx");
            Response.BinaryWrite(FileForDownload);
            Response.End();
        }
    }
}